#!/usr/bin/env python
#coding=utf-8
import requests
import json
import sys
import urllib2
import re

def get_hiddenvalue(url):
	request=urllib2.Request(url)
	reponse=urllib2.urlopen(request)
	resu=reponse.read()
	VIEWSTATE=re.findall(r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />',resu, re.I)
	EVENTVALIDATION =re.findall(r'input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />', resu,re.I)
	return VIEWSTATE[0],EVENTVALIDATION[0]
def get_hiddenvalue_string(myStr):
	VIEWSTATE=re.findall(r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />',myStr, re.I)
	EVENTVALIDATION =re.findall(r'input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />', myStr,re.I)
	return VIEWSTATE[0],EVENTVALIDATION[0]

reload(sys)
sys.setdefaultencoding( "utf-8" )

url = 'http://zjzx.zjnu.edu.cn/test/Default.aspx?cid=1&pid=1'


data = {
	'cid':'1',
	'pid':'1'
}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
}
#data['Button2']='提交并查看多选题答案'
#data['Button1']='提交并查看单选题答案'
#data['Button3']='提交并查看判断题答案'

data['Button1'] = '提交并查看单选题答案'
data['drop1']= '1'
data['drop2']='1'
data['__VIEWSTATE'],data['__EVENTVALIDATION'] = get_hiddenvalue(url)
response = requests.post(url=url,data=data,headers=headers)
responseReturn = response.text

del data['Button1']
data['__VIEWSTATE'],data['__EVENTVALIDATION']=get_hiddenvalue_string(responseReturn)
data['Button2']='提交并查看多选题答案'
response = requests.post(url=url,data=data,headers=headers)
responseReturn = response.text


del data['Button2']
data['__VIEWSTATE'],data['__EVENTVALIDATION']=get_hiddenvalue_string(responseReturn)
data['Button3']='提交并查看判断题答案'
response = requests.post(url=url,data=data,headers=headers)
responseReturn = response.text
#print responseReturn
#outFile=re.findall(r'<div id="timu">(.*?)<div id="foot" style="margin-top:5px;">浙江师范大学高校教师培训中心版权所有</div>',responseReturn, re.I)
#print outFile

with open('test.html','a') as file:
	file.write(responseReturn)

#print data
#data = json.loads(data)
